#!/usr/bin/perl -w
#
# Copyright(c) Plink Limited 2010. All rights reserved.
#
# $Header:$
#
# $Log:$
# 
# Author:      Jeremy Banks
#
# Description: Singleton logger
#

#----------------------------------------------------------------------
# External Modules
#----------------------------------------------------------------------

package XMLTV::Logger;

use strict;

use POSIX qw/strftime/;
use XMLTV::CacheCleaner;

#---------------------------------------------------------------------------
# Globals
#---------------------------------------------------------------------------

my $logger;
my $match_logger;
my $instance;
my $log_dir;

#---------------------------------------------------------------------------
# Get the singleton instance
#---------------------------------------------------------------------------

sub instance
{
   my $class = shift;
   if (!$instance)
   {
      $instance = bless {}, $class;
      _initialise_log();
      XMLTV::CacheCleaner::clean();
   }
   return $instance;
}

#---------------------------------------------------------------------------
# The directory to log to
#---------------------------------------------------------------------------

sub log_dir
{
   my ($self, $value) = @_;
   $log_dir = $value;
}

#---------------------------------------------------------------------------
# Initialise the log
#---------------------------------------------------------------------------

sub _initialise_log
{
   die "Can't instantiate the logger prior to setting the log directory" unless $log_dir;
   unless (-d $log_dir)
   {
      mkdir $log_dir or die "Couldn't make logging directory $log_dir: $!";
   }

   my $now = strftime "%Y%m%d_%H%M%S", localtime;
   my $filename = "$log_dir/xmltv_$now.log";
   open $logger, ">$filename" or die "Couldn't open log file $filename: $!";

   print "Using Log File: $filename\n";
   $filename = "$log_dir/xmltv_matches_$now.log";
   open $match_logger, ">$filename" or die "Couldn't open match log file $filename: $!";
}

#---------------------------------------------------------------------------
# Write a message to log
#---------------------------------------------------------------------------

sub log
{
   my ($self, $msg, $stdout) = @_;

   print "$msg\n" if $stdout;
   my $now = strftime "%Y-%m-%d %I:%M:%S.%p", localtime;
   print $logger "$now -> $msg\n";
   print $match_logger "$now -> $msg\n";
}

#---------------------------------------------------------------------------
# Write an error message to the log and die
#---------------------------------------------------------------------------

sub die
{
   my ($self, $msg) = @_;

   $self->log("Error: $msg", 1);
   exit;
}

#---------------------------------------------------------------------------
# Log match
#---------------------------------------------------------------------------

sub log_match
{
   my ($self, $msg) = @_;

   my $now = strftime "%Y-%m-%d_%I.%M.%S.%p", localtime;
   print $match_logger "$now -> $msg\n";
}

#---------------------------------------------------------------------------
# Destructor
#---------------------------------------------------------------------------

sub DESTROY
{
   my $self = shift;
   close $logger;
   close $match_logger;
}

1;
